using System.Web;
using System.Xml;

using Microsoft.SharePoint;

using RealDolmen.Helpers;
using RealDolmen.SharePoint.Helpers;

namespace RealDolmen.SharePoint.ListFeatures.Elements
{
    public class ListFeatureCustomActionElement : ListFeatureElement
    {
        public enum Keys
        {
            Title
        }

        public static string RootTag
        {
            get { return "CustomAction";  }
        }

        #region Overrides
        public override string FullKey
        {
            get { return ListFeatureKeyManager.GenerateDottedKey("CustomAction", Key); }
        }
        public override string Key
        {
            get { return Id; }
        }

        public override bool Customizable
        {
            get { return true; }
        }

        public override string GetDefaultElement(object name)
        {
            if (name == null)
                return "NA";
            else
                return GetDefaultAttribute(name.ToString(), "NA");
        } 
        #endregion // Overrides

        #region Activate / Deactivate
        public override void Activate(ListFeature listFeature)
        { }

        public override void Deactivate(ListFeature listFeature)
        { } 
        #endregion // Activate / Deactivate

        #region Public Properties
        public string Id
        {
            get { return GetDefaultAttribute("Id"); }
        }

        public string Location
        {
            get { return GetDefaultAttribute("Location"); }
        }

        public string GroupId
        {
            get { return GetDefaultAttribute("GroupId"); }
        }

        public string RegistrationType
        {
            get { return GetDefaultAttribute("RegistrationType"); }
        }

        public string RegistrationId
        {
            get { return GetDefaultAttribute("RegistrationId"); }
        }

        public bool RequireSiteAdministrator
        {
            get { return Helper.GetBooleanOrDefault(GetDefaultAttribute("RequireSiteAdministrator"), false); }
        }

        public string ImageUrl
        {
            get { return GetDefaultAttribute("ImageUrl"); }
        }

        public string Url
        {
            get
            {
                XmlElement element = XmlDefinition["UrlAction"];

                if (element != null)
                {
                    XmlAttribute attribute = element.Attributes["Url"];
                    if (attribute != null)
                        return attribute.Value;
                }
                return null;
            }
        }

        public string ParsedUrl
        {
            get
            {
                SPWeb parentWeb = Feature.ParentList.ParentWeb;

                string parsedUrl = Core.ReplaceUrlTokens(Url,
                    parentWeb,
                    Feature.ParentList,
                    SPContext.Current.ListItem,
                    Feature);

                // SPHelper.TryDisposeWeb(parentWeb);

                return parsedUrl;
            }
        }

        public string Sequence
        {
            get { return GetDefaultAttribute("Sequence"); }
        }

        public string Title
        {
            get { return GetElement(ListFeatureCustomActionElement.Keys.Title); }
            set { SetElement(ListFeatureCustomActionElement.Keys.Title, value); }
        }

        public string ParsedTitle
        {
            get
            {
                return Helper.GetHttpResource(Title);
            }
        }

        public string Rights
        {
            get { return GetDefaultAttribute("Rights"); }
        }

        public string Description
        {
            get { return GetDefaultAttribute("Description"); }
        }

        public string ParsedDescription
        {
            get
            {
                return Helper.GetHttpResource(Description);
            }
        } 
        #endregion // Public Properties
    }
}
